package com.hackerrank.challenges.validpanformat;

import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		in.nextLine();
		for(int n = 0; n < N; n++) {
			char c[] = in.nextLine().toCharArray();
			if(c.length != 10 || c[0] < 'A' || c[0] > 'Z' || c[1] < 'A' || c[1] > 'Z'
			|| c[2] < 'A' || c[2] > 'Z' || c[3] < 'A' || c[3] > 'Z' || c[4] < 'A' 
			|| c[4] > 'Z' || c[5] < '0' || c[5] > '9' || c[6] < '0' || c[6] > '9'
			|| c[7] < '0' || c[7] > '9' || c[8] < '0' || c[8] > '9' || c[9] < 'A'
			|| c[9] > 'Z')
				System.out.println("NO");
			else
				System.out.println("YES");
		}
	}
}
